<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>坤哥图书</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #e9ecef;
            margin: 0;
            padding: 20px;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
        }

        .container {
            background-color: #ffffff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px; /* Increased max-width */
            text-align: left; /* Align text to left */
        }

        h1 {
            color: #343a40;
            margin-bottom: 25px;
            text-align: center; /* Center align heading */
        }

        form {
            display: flex;
            flex-direction: column;
            gap: 10px; /* Reduced gap */
        }

        .form-group {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
        }

        label {
            font-weight: 600;
            color: #555;
            margin-bottom: 5px; /* Adjusted margin */
        }

        input[type="text"],
        input[type="number"] {
            width: 100%; /* Full width */
            padding: 10px;
            border: 1px solid #ced4da;
            border-radius: 5px;
            font-size: 1rem;
            box-sizing: border-box;
        }

        input[type="submit"] {
            background-color: #007bff;
            color: white;
            padding: 12px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 1.1rem;
            font-weight: 600;
            transition: background-color 0.3s ease;
            margin-top: 20px; /* Adjusted margin */
        }

        input[type="submit"]:hover {
            background-color: #0056b3;
        }

        .back-button {
            display: inline-block;
            background-color: #6c757d;
            color: white;
            padding: 10px 20px;
            border-radius: 5px;
            text-decoration: none;
            font-size: 1rem;
            margin-top: 30px; /* Adjusted margin */
            transition: background-color 0.3s ease;
            text-align: center;
            width: fit-content;
            margin-left: auto;
            margin-right: auto;
        }

        .back-button:hover {
            background-color: #5a6268;
        }

        .alert {
            padding: 15px;
            margin-bottom: 20px;
            border: 1px solid transparent;
            border-radius: 4px;
            text-align: left;
        }

        .alert-danger {
            color: #721c24;
            background-color: #f8d7da;
            border-color: #f5c6cb;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 th:text="${book.id == null ? '添加新书' : '编辑书籍'}">坤哥图书</h1>

    <form th:action="${book.id == null ? '/books' : '/books/update'}" th:object="${book}" method="post">
        <input type="hidden" th:field="*{id}" />
        <div class="form-group">
            <label for="title">书名:</label>
            <input type="text" id="title" th:field="*{title}" required />
        </div>

        <div class="form-group">
            <label for="author">作者:</label>
            <input type="text" id="author" th:field="*{author}" required />
        </div>

        <div class="form-group">
            <label for="isbn">ISBN:</label>
            <input type="text" id="isbn" th:field="*{isbn}" required />
        </div>

        <div class="form-group">
            <label for="price">价格:</label>
            <input type="number" id="price" th:field="*{price}" step="0.01" required />
        </div>

        <div class="form-group">
            <label for="location">书籍位置:</label>
            <input type="text" id="location" th:field="*{location}" />
        </div>

        <input type="submit" value="保存" />
    </form>
    <a th:href="@{/books}" class="back-button">返回列表</a>
    </div>

    <script th:inline="javascript">
        /*<![CDATA[*/
        var errorMessage = [[${errorMessage}]] || '';
        var successMessage = [[${successMessage}]] || '';
        if (errorMessage) {
            alert(errorMessage);
        } else if (successMessage) {
            alert(successMessage);
            window.location.href = '/books'; // 自动返回主页面
        }
        /*]]>*/
    </script>
</body>
</html>